package com.yyx.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyx.common.R;
import com.yyx.common.annotation.SystemLog;
import com.yyx.entity.Employee;
import com.yyx.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author yeyinxuan
 * @since 2023-07-31
 */

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        R b = employeeService.login(request,employee);
//        登录成功将用户ID存入session中
        return b;
    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
//        R b =
//        request.getSession().removeAttribute("employee");
//        request.getSession().removeAttribute("");
//        local
                return R.success("退出成功");
    }

    @PostMapping
    public R saveEmployee(HttpServletRequest request, @RequestBody Employee employee){
        boolean b = employeeService.saveEmployee(request,employee);
        if( b == false){
            return R.error("添加用户失败(账户ID已存在)");
        }
        else return R.success("添加成功");
    }
    /**
     * 禁用员工状态
     * @param employee
     * @return
     */
    @SystemLog(methodName = "更新员工信息")
    @PutMapping
    public R updateEmployee1(@RequestBody Employee employee){
        employeeService.updateEmployee1(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据ID查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getEmployeeById(@PathVariable("id") Long id){
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("系统错误");
    }

    @GetMapping("/page")
    public R<Page> getPage(int page,int pageSize,String name){
        Page<Employee> page1 = employeeService.selectpage(page, pageSize, name);
        return R.success(page1);
    }

}
